package com.duoduo.demo.upload;

import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("/upload")
public class UploadController {
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/simple", method = RequestMethod.GET)
    public String simple() {
        return "index.simple";
    }

    @RequestMapping(value = "/old", method = RequestMethod.GET)
    public String old() {
        return "index.old";
    }

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public String uploadFile(@RequestParam("file") CommonsMultipartFile multipartFile, HttpServletRequest request, HttpServletResponse response) throws IOException {
        return resopnseData(true, uploadFile(request, multipartFile));
    }

    @RequestMapping(value = "/uploadFiles", method = RequestMethod.POST)
    @ResponseBody
    public String uploadFiles(MultipartHttpServletRequest request) throws Exception{
        List<String> urlList = new ArrayList<String>(0);
        List<MultipartFile> files = request.getFiles("file");
        if(CollectionUtils.isNotEmpty(files)) {
            for(MultipartFile file : files) {
                urlList.add(uploadFile(request, file));
            }
        }

        return resopnseData(true, urlList);
    }

    private String uploadFile(HttpServletRequest request, MultipartFile multipartFile) throws IOException {
        //设置文件保存的本地路径
        String appRoot = getAppRoot(request);
        String fileUrl = getFileUrl(multipartFile);

        //将文件保存到服务器
        FileUtil.upFile(multipartFile.getInputStream(), fileUrl, appRoot);

        return fileUrl;
    }

    private String getAppRoot(HttpServletRequest request) {
        return request.getSession().getServletContext().getRealPath("");
    }

    private String getFileUrl(MultipartFile multipartFile) {
        return "/uploadFiles/" + generateUuid() + "/" + multipartFile.getOriginalFilename();
    }

    private String generateUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    private <T> String resopnseData(boolean success, T data) {
        Map<String, Object> resultMap = new HashMap<String, Object>(0);
        resultMap.put("success", success);
        resultMap.put("data", data);

        String jsonString = JSONObject.fromObject(resultMap).toString();
        System.out.println("resopnse json = " + jsonString);
        return jsonString;
    }
}
